package year2021.month0234;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class StreamTest {

    public static void main(String[] args){
        List<User> list = new ArrayList<User>(){{
            add(new User("好", 1));
            add(new User("好", 2));
            add(new User("学", 3));
            add(new User("习", 4));
            add(new User("好好学习", 5));
        }};
//        Map<String, User> map = list.stream().collect(Collectors.toMap(User::getName, Function.identity()));
        Map<String, User> map = list.stream().collect(Collectors.toMap(User::getName, Function.identity(), (v1, v2) -> v2));

        System.out.println(map);
    }

}

class User{
    private String name;
    private Integer age;

    public User(String name, Integer age){
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString(){
        return name + "'s age is " + age;
    }
}